package com.project.controller;

import com.project.bean.UserBean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

@Controller
@RequestMapping("user")
public class UserController {

    @RequestMapping("login")
    public String login(HttpServletRequest request,String userName,String pwd){

        System.out.println(userName+"  "+pwd);

        if ("java".equals(userName) && "123".equals(pwd)){

            //在会话对象中，添加登录信息
            request.getSession().setAttribute("userName",userName);

            return "redirect:/html/index.html";
        }
        return "redirect:/html/error.html";
    }

    @RequestMapping("add")
    public String add(UserBean userBean,
                      @RequestParam("myFile")MultipartFile mf){
//                      @RequestParam("myFile")MultipartFile mf

        String fileName = mf.getOriginalFilename();
        //将上传文件重命名
        fileName = System.currentTimeMillis() + fileName.substring(fileName.lastIndexOf("."));
        try {
            //将上传的文件保存到指定目录
            mf.transferTo(new File("F:/java作业文件夹/webTest/webFliterUpload/src/main/resources/static/html/img/"+fileName));

        } catch (Exception e) {
            e.printStackTrace();
        }

        userBean.setPic(fileName);
        System.out.println(userBean);
        return "redirect:/html/index.html";
    }
}
